<?php
declare(strict_types=1);

namespace App\Controller\Admin;

use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use App\Service\AdminService;
use \Psr\Http\Message\ResponseInterface;

class AdminController extends AbstractController
{
    /**
     * @Inject
     * @var AdminService
     */
    protected $adminService;

    public function index()
    {
        $searchParams = $this->request->post();
        $admins = $this->adminService->indexList($searchParams);
        $this->render->render('admin/admin/index.twig', [
            'admins' => $admins,
        ]);
    }

    public function info()
    {
        $id = $this->request->route('id');

        $admin = $this->adminService->queryById($id);
        return $this->render->render('admin/admin/show', [
            'info' => $admin,
        ]);
    }

    public function store()
    {

    }

    public function update()
    {

    }

    public function del()
    {

    }

    public function profile()
    {
        $admin = $this->adminService->currentLogin();

        return $this->render->render('admin/profile/index.twig', [
            'admin' => $admin,
        ]);
    }

    /**
     * 修改密码页面
     *
     * @return ResponseInterface
     */
    public function pagePassword()
    {
        return $this->render->render('admin/profile/password.twig');
    }

    /**
     * 密码修改
     *
     * @return ResponseInterface
     */
    public function passwordChange()
    {
        $oldPsw = $this->request->post('old_password');
        $newPsw = $this->request->post('new_password');
        $confirmPsw = $this->request->post('again_password');
        $res = $this->adminService->passwordChange($oldPsw, $newPsw, $confirmPsw);

        return $this->response->json($res);
    }
}